﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using SIR.Entities.Implementations;
using SIR.Web.API;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Http.Hosting;

namespace SIR.Web.Services.Tests
{
	[TestClass]
	public class ImageControllerTest
	{
		[TestInitialize]
		public void ConnectToDatabase()
		{
			// connect to the database
			DAL.DataServer.Connect(System.Configuration.ConfigurationManager.ConnectionStrings["SIR"].ConnectionString);
			// establish the HTTP context
			SimpleWorkerRequest request = new SimpleWorkerRequest("", "", "", null, new StringWriter());
			HttpContext context = new HttpContext(request);
			HttpContext.Current = context;
		}

		[TestCleanup]
		public void DisconnectFromTheDatabase()
		{
			// disconnect from the database
			DAL.DataServer.Disconnect();
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.UploadPicture, 1, typeof(ImageController))]
		public void UploadPicture_InvalidImageFormat()
		{
			// Create the image controller
			var controller = new ImageController();
			// Initalize the request
			controller.Request = new System.Net.Http.HttpRequestMessage();
			// Empty Content
			controller.Request.Content = new ByteArrayContent(new byte[0], 0, 0);
			controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
			var result = controller.PostFormData().Result;
			// Get the content of the result
			var content = ((ObjectContent<List<Response<Image>>>)(result.Content)).ReadAsAsync<List<Response<Image>>>().Result;
			// Submitted content was unsupported media
			Assert.IsNotNull(content);
			Assert.IsFalse(content[0].Successful);
			Assert.AreEqual(content[0].Message, "Unsupported Media Type");
			// Get the text file
			var fileName = Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\SIR.Web.Tests\\Content\\TextFile.txt");
			// Set the content as the contexts of the text file
			var formDataContent = new MultipartFormDataContent();
			var fileContent = new StreamContent(new MemoryStream(System.IO.File.ReadAllBytes(fileName)));
			// Set as plain text
			fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
			// Add a file content with the text
			formDataContent.Add(fileContent, "\"file\"", fileName);
			controller.Request.Content = formDataContent;
			// post the action
			result = controller.PostFormData().Result;
			// get the results
			content = ((ObjectContent<List<Response<Image>>>)(result.Content)).ReadAsAsync<List<Response<Image>>>().Result;
			// Text is unsupported
			Assert.IsNotNull(content);
			Assert.AreEqual(content[0].Message, "Unsupported Media Type");
			Assert.IsFalse(content[0].Successful);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.UploadPicture, 2, typeof(ImageController))]
		public void UploadPicture_ValidImageFormat()
		{
			// Create image controller
			var controller = new ImageController();
			// Intialize the request
			controller.Request = new System.Net.Http.HttpRequestMessage();
			controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
			// Get the image to upload
			var fileName = Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\SIR.Web.Tests\\Content\\TestImage.jpg");
			var formDataContent = new MultipartFormDataContent();
			// Set the image as the content
			var fileContent = new StreamContent(new MemoryStream(System.IO.File.ReadAllBytes(fileName)));
			fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/JPEG");
			// Set the image as private
			var textContent = new StreamContent(new MemoryStream(System.Text.UnicodeEncoding.UTF8.GetBytes("")));
			formDataContent.Add(fileContent, "\"file\"", fileName);
			formDataContent.Add(textContent, "\"private\"", "private");
			// Set the content and post the data
			controller.Request.Content = formDataContent;
			var result = controller.PostFormData().Result;
			// Get the response content
			var content = ((ObjectContent<List<Response<Image>>>)(result.Content)).ReadAsAsync<List<Response<Image>>>().Result;
			// verify the results were successful
			Assert.IsNotNull(content);
			Assert.AreEqual(content[0].Message, "");
			Assert.IsTrue(content[0].Successful);
			// Get the image from the response and delete it
			var image = content[0].Content as Image;
			image.IsDeleted = true;
			DAL.DataServer.Save(image);
		}
	}
}
